import { createRouter, createWebHashHistory } from 'vue-router'
import indexPage from '@/views/indexPage.vue'
import loginPage from '@/views/loginPage.vue'
import acManagePage from '@/views/acManagePage.vue'
import acDetail from '@/views/acDetail.vue'
import growthProfile from '@/views/growthProfile.vue'
import profileDetail from '@/views/profileDetail.vue'
import settings from "@/views/settings.vue";
const routes = [
  {
    path: '/',
    component: loginPage
  },
  {
    path: '/indexPage',
    component: indexPage,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/acManagePage',
    component: acManagePage,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/acManagePage/acDetail/:id',
    name: 'acDetailInfo',
    component: acDetail,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/growthProfile',
    component: growthProfile,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/profileDetail',
    component: profileDetail,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/setting',
    component:settings,
    meta: {
      requiresAuth: true
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(), // 路径要带#号，createWebHistory路径不用带#号
  routes
})


router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 判断用户是否已登录，三十分鐘后登錄過期
    if (!isUserLoggedIn()) {
      next({
        path: '/'
      });
    } else {
      next();
    }
  } else {
    next();
  }
});

function isUserLoggedIn() {
  let time = localStorage.getItem("user")
  let left = (new Date().getTime()-time)/1000/60
  console.log(left)
  if(left<=30){
    return true
  }else{
    localStorage.setItem("user",0)
    return false
  }
}

export default router
